const svc = require('../util/service')
const logger = require('log4js').getLogger()
const config = require('../util/config')

module.exports = (req, res) => {
    svc.handlePost(req, res, (reqObj, resolve) => {
        const mongoMng = require('../mng/mongoMng')
        if (!mongoMng.db) {
            logger.error('[' + __filename + ':' + __line + ']', '未连接mongo')
            resolve({
                result: -1,
                msg: '系统错误'
            })
            return
        }
        mongoMng.db.collection('user').find({
            isPromoter: true
        }).toArray((err, result) => {
            if (err) {
                logger.error('[' + __filename + ':' + __line + ']', err)
                resolve([])
                return
            }
            var promises = []
            result.forEach(item => {
                promises.push(new Promise(resolve => {
                    var promise0 = new Promise(resolve => {
                        mongoMng.db.collection('user').find({
                            promoter: item.phone,
                            registerTime: {
                                $gte: new Date('2019/04/01'),
                                $lt: new Date('2019/07/01')
                            }
                        }).toArray((err, result) => {
                            if (err) {
                                logger.error('[' + __filename + ':' + __line + ']', err)
                                resolve([])
                                return
                            }
                            var promotees = []
                            result.forEach(item => {
                                promotees.push({
                                    phone: item.phone,
                                    hasBill: false
                                })
                            })
                            resolve(promotees)
                        })
                    }),
                        promise1 = new Promise(resolve => {
                            mongoMng.db.collection('bill').aggregate([{
                                $match: {
                                    returnPromoterMoney: {
                                        $ne: null
                                    }
                                }
                            }, {
                                $group: {
                                    _id: '$customer_phone',
                                    returnPromoterMoney: {
                                        $sum: '$returnPromoterMoney'
                                    }
                                }
                            }]).toArray((err, result) => {
                                if (err) {
                                    logger.error('[' + __filename + ':' + __line + ']', err)
                                    resolve([])
                                    return
                                }
                                resolve(result)
                            })
                        }),
                        promise2 = new Promise(resolve => {
                            mongoMng.db.collection('bill').aggregate([{
                                $match: {
                                    returnPromoterMoney: null,
                                    payStatus: 3
                                }
                            }, {
                                $group: {
                                    _id: '$customer_phone',
                                    feeString: {
                                        $addToSet: '$feeString'
                                    }
                                }
                            }]).toArray((err, result) => {
                                if (err) {
                                    logger.error('[' + __filename + ':' + __line + ']', err)
                                    resolve([])
                                    return
                                }
                                resolve(result)
                            })
                        })
                    Promise.all([promise0, promise1, promise2]).then(result => {
                        var promotees = result[0]

                        promotees.forEach(item => {
                            for (var i = 0; i < result[1].length; ++i) {
                                if (result[1][i]._id == item.phone) {
                                    item.hasBill = true
                                    break
                                }
                            }
                        })

                        promotees.forEach(item => {
                            for (var i = 0; i < result[2].length; ++i) {
                                if (result[2][i]._id == item.phone) {
                                    result[2][i].feeString.forEach(_item => {
                                        item.hasBill = true
                                    })
                                    break
                                }
                            }
                        })

                        resolve({
                            promoter_phone: item.phone,
                            promotees
                        })
                    })
                }))
            })
            Promise.all(promises).then(result => {
                resolve(result)
            })
        })
    })
}
